//
//  GuiderDetialHeadCell.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/1/5.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "GuiderDetialHeadCell.h"
#import "SDPhotoBrowser.h"
#import "IGAvPlayerManager.h"
@interface GuiderDetialHeadCell () <SDPhotoBrowserDelegate>

@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UIImageView *guiderImageView;
@property (weak, nonatomic) IBOutlet UILabel *guiderNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *guiderTagLabel;
@property (weak, nonatomic) IBOutlet UIImageView *guiderVipTagImageView;
@property (weak, nonatomic) IBOutlet UILabel *guiderDescribeLabel;
@property (weak, nonatomic) IBOutlet UILabel *followCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *listenCountLabel;

@property (weak, nonatomic) IBOutlet UIButton *followBtn;

@property (weak, nonatomic) IBOutlet UIView *audioView;
@property (weak, nonatomic) IBOutlet UIImageView *audioImageView;
@property (weak, nonatomic) IBOutlet UILabel *audioLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *audioViewH;


@end

@implementation GuiderDetialHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.audioLabel.text = MYLocalizedString(@"GUIDELIST_AUDIO_INFO", nil);
    NSMutableArray *imageArray = [NSMutableArray array];
    
    for (int i = 1; i <= 3; i ++) {
        
        UIImage *image = [UIImage imageNamed:[@"speech_" stringByAppendingString:@(i).stringValue]];
        
        [imageArray addObject:image];
    }
    
    self.audioImageView.animationImages = imageArray;
    self.audioImageView.animationDuration = 1.0;
    self.audioImageView.animationRepeatCount = 0;
    
    
    self.guiderImageView.layer.borderWidth = 1.0f;
    self.guiderImageView.layer.borderColor = kBGColor.CGColor;
    
    @weakify(self);
    [self.guiderImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        
        @strongify(self);
        SDPhotoBrowser *browser = [SDPhotoBrowser new];
        browser.sourceImagesContainerView = self.guiderImageView;
        browser.imageCount = 1;
        browser.currentImageIndex = 0;
        browser.delegate = self;
        [browser show];
    }]];
    [self.audioView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        
        @strongify(self);
//        [self startPlay];
        
        if (self.audioImageView.isAnimating) {
            [[IGAvPlayerManager shareManeger] pause];
            [self stopPlay];
            
            return ;
        }
        
        if ([[IGAvPlayerManager shareManeger].currentURL isEqualToString:self.guider.audiointroduction]) {
            [[IGAvPlayerManager shareManeger] play];
        } else {
            
            [[IGAvPlayerManager shareManeger] playMusic:self.guider.audiointroduction withPlayType:PlayAnother];
        }
        
        [self startPlay];
    }]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAudioState:) name:kIGAvPlayerManagerState object:nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changeAudioState:(NSNotification *)notif {
    
    NSInteger state = [notif.userInfo[kIGAvPlayerManagerState] integerValue];
    NSString  *url = notif.userInfo[kCurrentPlayURL];
    
    NSNumber * type = notif.userInfo[kPlayType];
    if (![url isEqualToString:self.guider.audiointroduction] || type.integerValue != PlayAnother) {
        
        return;
    }
    
    switch (state) {
        case AVPlayerPlayStatePause:
        case AVPlayerPlayStateEnd:
        {
            [self stopPlay];
        }
            break;
    }
}


- (void)startPlay {
    
    [self.audioImageView startAnimating];
}

- (void)stopPlay {
    
    [self.audioImageView stopAnimating];
}

- (IBAction)followAction:(UIButton *)sender {
    
    if (![IGUtil hasLoginOrPresentLogin]) {
        return;
    }
    
    sender.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        sender.userInteractionEnabled = YES;
    });
    
    @weakify(self);
    
    
    [NetAPIManager collectWithType:CollectTypeGuider favourid:self.guider.guideid block:^(id result, NSError *error) {
        @strongify(self);
        
        BOOL isfavor = [result boolValue];
        
        self.guider.isfavor = isfavor;
        
        self.followBtn.selected = isfavor;
        
        if (isfavor) {
            [IGUtil showToast:MYLocalizedString(@"GUIDELIST_FOLLOW", nil)];
            
        } else{
            [IGUtil showToast:MYLocalizedString(@"GUIDELIST_FOLLOWCANCEL", nil)];
        }
        
    }];
    
}
- (void)setGuider:(GuiderModel *)guider {
    
    _guider = guider;
    
    [self.guiderImageView setImageURL:[NSURL URLWithString:guider.headimg] placeholderImage:[UIImage imageNamed:@"default"]];
    self.guiderNameLabel.text = guider.realname;
    self.guiderTagLabel.text = guider.certificate;
    self.guiderDescribeLabel.text = guider.introduction;
    self.followCountLabel.text = [NSString stringWithFormat:MYLocalizedString(@"FOLLOW_COUNT", nil),guider.favourcount];
    self.listenCountLabel.text = [NSString localizedStringWithFormat:MYLocalizedString(@"LISTEN_COUNT", nil),guider.visitcount];
    self.followBtn.selected = guider.isfavor;
    self.guiderVipTagImageView.hidden = !guider.vip;
    
    if (guider.audiointroduction.length == 0) {

        self.audioImageView.hidden = YES;
        self.audioLabel.hidden = YES;
        self.audioView.hidden = YES;
        self.audioViewH.constant = 10;

    }
    
    if ([[IGAvPlayerManager shareManeger].currentURL isEqualToString:self.guider.audiointroduction] && [IGAvPlayerManager shareManeger].isPlay) {
        [self startPlay];
    } else {
        [self stopPlay];
    }
}

+ (CGFloat)getCellHeightWithContentText:(NSString *)text {
    
    CGSize size = [IGUtil getSizeContent:text width:kScreenWidth - 32 font:Font(12)];
    
    return 164 + size.height;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    
    return self.guiderImageView.image;
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    
    return [NSURL URLWithString:self.guider.headimg];
}

@end
